﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractForeEnd;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.Logss;
using HamcoWcs.Entitys.MesMsgQueues;
using HamcoWcs.Models.BaseConfigs;
using HamcoWcs.Models.Common;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Logs;
using HamcoWcs.Models.Logss;
using HamcoWcs.Models.OperationLogs;
using HamcoWcs.Service.Common;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HamcoWcs.Service.ForeEnd
{
    [AutoInject(typeof(ILogViewService), InjectType.Scope)]
    public class LogViewService : ForeEndBaseService, ILogViewService
    {
        public LogViewService(WcsDbContext wcsDbContext, IMapper mapper, ILoggerService logger) : base(wcsDbContext, mapper, logger)
        {
        }

        public ForeEndResult GetDeviceErrorLog(QueryDeviceErrorLog query)
        {
            var queryable = _wcsDbContext.DeviceErrorLogs
                                         .AsQueryable()
                                         .OrderByDescending(d => d.CreatedTime)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .ToList();

            return new ForeEndResult(queryable, count, pageCount);

        }

        public ForeEndResult GetDeviceMsgLog(QueryDeviceMsgLog query)
        {
            var queryable = _wcsDbContext.DeviceMsgLogs
                                         .AsQueryable()
                                         .OrderByDescending(d => d.CreatedTime)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .ToList();

            var dataDtos = _mapper.Map<List<DeviceMsgLogDto>>(queryable);
            return new ForeEndResult(dataDtos, count, pageCount);

        }

        public ForeEndResult GetLoginLog(QueryLoginLog query)
        {
            var queryable = _wcsDbContext.LoginLogs
                                         .AsQueryable()
                                         .OrderByDescending(d => d.CreatedTime)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .ToList();

            var dataDtos = _mapper.Map<List<LoginLogDto>>(queryable);
            return new ForeEndResult(dataDtos, count, pageCount);
        }

        public ForeEndResult GetOperationLog(QueryOperationLog query)
        {

            var queryable = _wcsDbContext.OperationLogs
                                         .AsQueryable()
                                         .OrderByDescending(d => d.CreatedTime)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .ToList();

            var dataDtos = _mapper.Map<List<OperationLogDto>>(queryable);
            return new ForeEndResult(dataDtos, count, pageCount);

        }

        public ForeEndResult GetSystemErrorLog(QuerySystemErrorLog query)
        {
            var queryable = _wcsDbContext.SystemErrorLogs
                                         .AsQueryable()
                                         .OrderByDescending(d => d.CreatedTime)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .ToList();

            var dataDtos = _mapper.Map<List<SystemErrorLogDto>>(queryable);
            return new ForeEndResult(dataDtos, count, pageCount);
        }

        public ForeEndResult GetWcsRunLog(QueryWcsRunLog query)
        {
            var queryable = _wcsDbContext.WcsRunLogs
                                         .AsQueryable()
                                         .OrderByDescending(d => d.CreatedTime)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .ToList();

            var dataDtos = _mapper.Map<List<WcsRunLogDto>>(queryable);
            return new ForeEndResult(dataDtos, count, pageCount);
        }


        public ForeEndResult GetBaseCongfigValue(BaseConfigPage query)
        {
            var queryable = _wcsDbContext.BaseConfigs
                                         .AsQueryable()
                                         .Where(x => !x.IsBackground) // 非后台配置
                                         .OrderBy(x => x.ConfigKey)
                                         .VagueWhereOut(query, out int count, out int pageCount)
                                         .ToList();

            var dataDtos = _mapper.Map<List<BaseConfigDto>>(queryable);
            return new ForeEndResult(dataDtos, count, pageCount);
        }

        //public Task<MesMsgLogResult> GetMesCommunicationLog(MesMsgLogQueryDto query)
        //{
        //    // todo 有bug
        //    var queryable = _wcsDbContext.MesMsgQueues
        //                                 .WhereIf(d => d.Sender == query.Sender, query.Sender != null)
        //                                 .WhereIf(d => d.Status == query.Status, query.Status != null)
        //                                 .WhereIf(d => d.RequestType == query.RequestType, query.RequestType != null)
        //                                 .WhereIf(d => d.EventName.Contains(query.EventName), !string.IsNullOrEmpty(query.EventName?.Trim()))
        //                                 .WhereIf(d => d.CreatedTime >= query.StartTime, query.StartTime != default(DateTime))
        //                                 .WhereIf(d => d.CreatedTime <= query.EndTime, query.EndTime != default(DateTime))
        //                                 .ToList();

        //    var baks = _wcsDbContext.MesMsgLogs
        //                            .WhereIf(d => d.Sender == query.Sender, query.Sender != null)
        //                            .WhereIf(d => d.Status == query.Status, query.Status != null)
        //                            .WhereIf(d => d.RequestType == query.RequestType, query.RequestType != null)
        //                            .WhereIf(d => d.EventName.Contains(query.EventName), !string.IsNullOrEmpty(query.EventName?.Trim()))
        //                            .WhereIf(d => d.CreatedTime >= query.StartTime, query.StartTime != default(DateTime))
        //                            .WhereIf(d => d.CreatedTime <= query.EndTime, query.EndTime != default(DateTime))
        //                            .ToList();



        //    queryable.AddRange(_mapper.Map<List<MesMsgQueue>>(baks));

        //    var data = queryable.AsQueryable()
        //                        .OrderByDescending(d => d.CreatedTime)
        //                        .Pagion(query.Page, query.Size, out int count, out int pageCount)
        //                        .ToList();

        //    var dataDtos = _mapper.Map<List<MesMsgLogDto>>(data);

        //    return Task.Run(() => new MesMsgLogResult(count, pageCount, dataDtos));

        //}

        //public async Task<MesMsgLogResult> GetMesCommunicationLogBak(MesMsgLogQueryDto query)
        //{
        //    var baks = await _wcsDbContext.MesMsgLogs
        //                            .AsQueryable()
        //                            .OrderByDescending(d => d.CreatedTime)
        //                            .VagueWhereOut(query, out int count, out int pageCount)
        //                            .ToListAsync();

        //    var data = _mapper.Map<List<MesMsgLogDto>>(baks);
        //    return new MesMsgLogResult(count, pageCount, data);
        //}

        public ForeEndResult GetCommunicationLog(UICommunicationLogQueryModel model)
        {
            var query = _wcsDbContext.Set<CommunicationLog>()
                .AsNoTracking()
                .OrderByDescending(d => d.CreatedTime)
                .VagueWhereOut(model, out int count, out int pageCount)
                .ToList();

            var data = _mapper.Map<List<UICommunicationLog>>(query);

            return new ForeEndResult(data, count, pageCount);
        }
    }
}
